package com.sugon.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.RandomUtil;
import com.sugon.dao.DevAlarmDao;
import com.sugon.entity.DevAlarmEntity;
import com.sugon.entity.DevTypeEntity;
import com.sugon.entity.FactoryTypeEntity;
import com.sugon.service.DevAlarmService;
import com.sugon.service.DevTypeService;
import com.sugon.service.FactoryTypeService;
import com.sugon.utils.RRException;
import com.sugon.utils.excel.ExcelImport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 设备报警码信息表Service实现类
 *
 * @author YuXD
 * @date 2020-05-17 13:41:21
 */

@Service("devAlarmService")
public class DevAlarmServiceImpl implements DevAlarmService {
    private Logger log = LoggerFactory.getLogger(getClass());
    @Autowired
    private DevAlarmDao devAlarmDao;
    @Autowired
    private FactoryTypeService factoryTypeService;
    @Autowired
    private DevTypeService devTypeService;

    /**
     * queryObject
     *
     * @param id 主键
     * @return
     */
    @Override
    public DevAlarmEntity queryObject(String id) {
        return devAlarmDao.queryObject(id);
    }

    /**
     * queryList
     *
     * @param map 参数
     * @return
     */
    @Override
    public List<DevAlarmEntity> queryList(Map<String, Object> map) {
        return devAlarmDao.queryList(map);
    }

    /**
     * queryTotal
     *
     * @param map 参数
     * @return
     */
    @Override
    public int queryTotal(Map<String, Object> map) {
        return devAlarmDao.queryTotal(map);
    }

    /**
     * save
     *
     * @param devAlarm 实体
     * @return
     */
    @Override
    public int save(DevAlarmEntity devAlarm) {
        devAlarm.setId(RandomUtil.randomString(32));
        return devAlarmDao.save(devAlarm);
    }

    /**
     * update
     *
     * @param devAlarm 实体
     * @return
     */
    @Override
    public int update(DevAlarmEntity devAlarm) {
        return devAlarmDao.update(devAlarm);
    }

    /**
     * delete
     *
     * @param id
     * @return
     */
    @Override
    public int delete(String id) {
        return devAlarmDao.delete(id);
    }

    /**
     * deleteBatch
     *
     * @param ids
     * @return
     */
    @Override
    public int deleteBatch(String[] ids) {
        return devAlarmDao.deleteBatch(ids);
    }

    /**
     * imports
     *
     * @param file
     * @return
     */
    @Override
    public int imports(MultipartFile file) {
        if (file.isEmpty()) {
            throw new RRException("上传文件不能为空");
        }
        List<String[]> list = ExcelImport.getExcelData(file);
        int num = 0;
        for (int i = 0; i < list.size(); i++) {
            int size = i + 1;
            if (size < list.size()) {
                String[] ftArr = list.get(size);
                if (ftArr.length < 4) {
                    continue;
                }
                // 校验下设备类型是否存在
                Map<String, Object> devTypeParam = new HashMap<>();
                devTypeParam.put("code", ftArr[0]);
                List<DevTypeEntity> devTypes = devTypeService.queryList(devTypeParam);
                if (CollectionUtil.isEmpty(devTypes)) {
                    log.info("设备类型[{}]尚未添加，请添加后再进行设置", ftArr[0]);
                    continue;
                }
                // 校验下该公司是否已存在
                Map<String, Object> param = new HashMap<>(2);
                param.put("name", ftArr[1]);
                param.put("type", 0);
                List<FactoryTypeEntity> factoryTypes = factoryTypeService.queryList(param);
                if (CollectionUtil.isEmpty(factoryTypes)) {
                    log.info("供应厂商[{}]尚未添加，请添加后再进行设置", ftArr[1]);
                    continue;
                }
                DevAlarmEntity alarm = new DevAlarmEntity();
                alarm.setId(RandomUtil.randomString(32));
                alarm.setDevType(ftArr[0]);
                alarm.setDevTypeName(devTypes.get(0).getTypeName());
                alarm.setFaName(ftArr[1]);
                alarm.setFaCode(factoryTypes.get(0).getFaCode());
                alarm.setCode(ftArr[2]);
                alarm.setMsg(ftArr[3]);
                // 保存前校验下是否已存在，如果已存在，不执行后续逻辑
                List<DevAlarmEntity> alarms = devAlarmDao.getByCondition(alarm.getFaCode(), alarm.getDevType(), alarm.getCode());
                if (CollectionUtil.isNotEmpty(alarms)) {
                    continue;
                }
                save(alarm);
                num++;
            }
        }
        return num;
    }


}
